<template>
  <el-dialog
    title="包月Vip购买"
    :visible.sync="dialogVisible"
    width="350px">
    <div v-if="dialogVisible">
      <div v-if="payType===0">账户余额: <span class="red b" v-text="'￥'+ToMoney(currMoney)"></span></div>
      <div v-if="vipTime && vipTime.length>0" style="margin-top: 5px;">会员到期时间 <span class="orange" v-text="vipTime"></span></div>

      <div style="padding-top: 10px; padding-bottom: 10px">
        <el-button :type="value.nums===form.nums?'warning':''" v-for="(value,index) in monthArr" :key="index"
                   @click="setMonthNums(value)"> {{ value.name }}
        </el-button>
      </div>
      <div>需付金额: <span class="red b" v-text="'￥'+ToMoney(form.money)"></span></div>
      <div class="pay-choose-box">
        <div class="title">支付方式</div>
        <div class="item" @click="payType=0">
          <div class="left">
            <div class="icon money-pay"></div>
            <span>余额支付</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===0?'on':''"></div>
          </div>
        </div>
<!--        <div class="item" @click="payType=1">
          <div class="left">
            <div class="icon wechat-pay"></div>
            <span>微信</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===1?'on':''"></div>
          </div>
        </div>
        <div class="item" @click="payType=2">
          <div class="left">
            <div class="icon ali-pay"></div>
            <span>支付宝</span>
          </div>
          <div class="right">
            <div class="pay-choose-icon" :class="payType===2?'on':''"></div>
          </div>
        </div>-->

        <div style="padding-top: 20px; font-size: 12px;">注: 如果想了解会员权益，请点击 <el-button type="warning" @click="showVipInfo">会员Vip介绍</el-button></div>
      </div>

    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确认支付</el-button>
    </div>
  </el-dialog>

</template>

<script>
export default {
  name: 'BuyVipDialog',
  data: function () {
    return {
      userInfo:{},
      callback: null,
      dialogVisible: false,
      payType: 0,
      currMoney: 0,
      vipTime: 0,
      monthArr: [],
      form: {
        nums: 1,
        money: 0,
      }
    }
  },
  mounted () {
    let $this = this
    var arr = []

    Object.keys(dict.VipMonthlyMoneyList).forEach(function (key, index) {
      arr.push({
        nums: key - 0,
        name: key + '个月',
        money: dict.VipMonthlyMoneyList[key],
      })
    })
    $this.monthArr = arr
    $this.form.money=$this.monthArr[0].money
    $this.form.nums=$this.monthArr[0].nums
    $this.bus.$on('BackKeyCallBus', function () {
      $this.dialogVisible = false
      $this.$forceUpdate()
    })
    $this.bus.$on('BuyVipDialogCallBus', function (callback,userInfo) {
      if ($this.dialogVisible){
        return
      }
      $this.payType=0
      $this.nums=1
      $this.dialogVisible = true
      $this.callback = callback
      $this.userInfo = userInfo
      $this.getCurrMoney()
      $this.$forceUpdate()
    })

  },
  methods: {
    showVipInfo:function () {
      // console.log('this.userInfo',this.userInfo)
      this.bus.$emit('LevelInfoDialogCallBus',this.userInfo.level)
    },
    setMonthNums:function (value) {
      this.form.nums=value.nums
      this.form.money=value.money
    },
    confirm: function () {
      let $this = this
      if (this.payType === 0) {
        if ($this.currMoney < $this.form.money) {
          return $this.successTip('当前账户余额不足', 'warning')
        }
      }
      // 对公支付
      /* if ($this.payType===3){
        $this.$router.push({
          name: 'EnterprisePayInfo',
          query: {
            type: 'order',
            orderNo: $this.order.orderNo,
            orderData: encodeURI(JSON.stringify({
              productList: $this.order.productData,
              price: $this.order.price,
            }))
          }
        })
        $this.dialogVisible = false
        return
      } */
      $this.MyRequest($this.ApiDoNameMember + 'Member/buyVip', {
        money: $this.form.money,
        payType: $this.payType,
      }, function (data) {
        // 1.账户余额支付,支付成功
        if ($this.payType === 0) {
          if (typeof $this.callback === 'function') {
            $this.dialogVisible = false
            $this.callback()
          }
          return
        }

        if ($this.payType === 1) {
          // 微信支付
          /* var wxPayPlus = api.require('wxPayPlus')
          wxPayPlus.payOrder({
            apiKey: data.appid,
            orderId: data.prepayid,
            mchId: data.partnerid,
            nonceStr: data.noncestr,
            timeStamp: data.timestamp,
            package: data.package,
            sign: data.sign
          }, function (ret, err) {
            if (ret.status) {
              $this.$message.success('支付成功')
              if (typeof $this.callback === 'function') {
                $this.callback()
              }
              $this.dialogVisible=false
              $this.$forceUpdate()
            } else {
              $this.$message.info('用户取消支付')
              // api.alert({
              //   title: '支付结果',
              //   msg: '支付失败: ' + JSON.stringify(err)
              // })
            }
          }) */

          $this.pay('wxpay', {
            appid: data.appid,
            prepayid: data.prepayid,
            partnerid: data.partnerid,
            noncestr: data.noncestr,
            timestamp: data.timestamp,
            package: data.package,
            sign: data.sign
          },function () {
            $this.$message.success('支付成功')
            if (typeof $this.callback === 'function') {
              $this.callback()
            }
            $this.dialogVisible=false
            $this.$forceUpdate()
          },function () {
            $this.$message.info('未完成支付')
          })
          /* var channels=false
          var currWxPayChannel=false
          plus.payment.getChannels(function(s){
            channels = s;
            var nameArr=[]
            for(var i in channels){
              var channel=channels[i];
              if (channel.id==='wxpay'){
                currWxPayChannel=channel
              }
              nameArr.push(channel.id)
            }
            var payParams={
              appid: data.appid,
              prepayid: data.prepayid,
              partnerid: data.partnerid,
              noncestr: data.noncestr,
              timestamp: data.timestamp,
              package: data.package,
              sign: data.sign
            }
            // alert('params: '+JSON.stringify(payParams))
            plus.payment.request(currWxPayChannel, payParams, function () {
              $this.$message.success('支付成功')
              if (typeof $this.callback === 'function') {
                $this.callback()
              }
              $this.dialogVisible=false
              $this.$forceUpdate()
            }, function (res) {
              $this.$message.info('未完成支付')
              // alert('==== 微信支付失败 ====:'+JSON.stringify(res))
            });

            // alert('通道名称: '+JSON.stringify(nameArr))
          }, function(e){
            alert("获取支付通道列表失败："+e.message);
          }); */
          return
          //
          // if (typeof $this.callback === 'function') {
          //   $this.callback()
          // }
        }

        if ($this.payType === 2) {
          // $this.dialogVisible=false
          // $this.$forceUpdate()
          // if (typeof api!=='undefined'){
            // 支付宝支付
            /* var aliPayPlus = api.require('aliPayPlus')
            aliPayPlus.payOrder({
              orderInfo: data
            }, function (ret, err) {
              setTimeout(function () {
                $this.$confirm('您是否支付完成?', '系统提示', {
                  confirmButtonText: '已经支付',
                  type: 'warning'
                }).then(() => {
                  if (typeof $this.callback === 'function') {
                    $this.callback()
                  }
                }).catch(() => {
                  if (typeof $this.callback === 'function') {
                    $this.callback()
                  }
                })
              },2500)
            }) */


            /* var channels=false
            var currWxPayChannel=false
            plus.payment.getChannels(function(s){
              channels = s;
              var nameArr=[]
              for(var i in channels){
                var channel=channels[i];
                if (channel.id==='alipay'){
                  currWxPayChannel=channel
                }
                nameArr.push(channel.id)
              }
              // alert('params: '+JSON.stringify(payParams))
              plus.payment.request(currWxPayChannel, data, function () {
                $this.$message.success('支付成功')
                if (typeof $this.callback === 'function') {
                  $this.callback()
                }
                $this.dialogVisible=false
                $this.$forceUpdate()
              }, function (res) {
                $this.$message.info('未完成支付')
                // alert('==== 微信支付失败 ====:'+JSON.stringify(res))
              });

              // alert('通道名称: '+JSON.stringify(nameArr))
            }, function(e){
              alert("获取支付通道列表失败："+e.message);
            }); */


          $this.pay('alipay',data,function () {
            $this.$message.success('支付成功')
            $this.dialogVisible=false
            if (typeof $this.callback === 'function') {
              $this.callback()
            }
            $this.$forceUpdate()
          },function () {
            $this.$message.info('未完成支付')
          })


          }


        // }

        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })

    },
    // 获取当前账户余额
    getCurrMoney: function () {
      let $this = this
      $this.currMoney=0
      $this.vipTime=''
      $this.MyRequest($this.ApiDoNameMember + 'Member/info', {}, function (data) {
        $this.currMoney = data.money
        if (data.vip){
          $this.vipTime = data.vipTime
        }
        $this.$forceUpdate()
      }, function (msg) {
        $this.$message.error(msg)
      })
    }
  }
}
</script>

<style scoped>

</style>
